/**
 * Created by Administrator on 2016/8/26.
 */
var sqlClient = require('../lib/mysql/mysql');
var rPacketDao = module.exports;
var moment = require('moment');


/*
*
* 获取所有可用系统红包，state = 1,status = 2
*
* */
rPacketDao.getAllSysRPList = function(cb){
    var sql = '';
    var sqlSelect = 'select *' ;
    var sqlFrom = ' from rb_sys_red_packets';
    var sqlCondition = ' where state = 1 and status = 2';
    var sqlSort = ' order by next_time asc'
    sql = sqlSelect + sqlFrom + sqlCondition + sqlSort;
    console.log(sql);
    sqlClient.query(sql,null, function(err, data){
        if(err){
            console.log(err);
            return cb && cb(err, null);
        }
        return cb && cb(null, data);
    })
}

/*
 * 系统红包列表，分页显示
 *
 * */
rPacketDao.getSysRPList = function(page,size,cb){
    var sql = '';
    var fields = [
        'red_packet_type',
        'rb_sys_red_packets.task_id',
        'total_money',
        'total_quantity',
        'time_interval',
        'rb_sys_red_packets.status',
        'rb_sys_red_packets.state',
        'task_name'


    ];
    var sqlSelect = 'select ' + fields.join(',');
    var sqlFrom = ' from rb_sys_red_packets ';
    var sqlJoin = 'left join rb_tasks on rb_sys_red_packets.task_id = rb_tasks.task_id';
    var sqlCondition = ' where rb_sys_red_packets.state = 1';
    var sqlSort = ' order by sys_red_packet_id asc limit ?,?'
    sql = sqlSelect + sqlFrom + sqlJoin + sqlCondition + sqlSort;
    console.log(sql);
    sqlClient.query(sql,[parseInt((page - 1) * size), parseInt(size)], function(err, data){
        if(err){
            console.log(err);
            return cb && cb(err, null);
        }
        return cb && cb(null, data);
    })
}

rPacketDao.getSysRPCount = function(cb){
    var sql = '';
    var sqlSelect = 'select count(sys_red_packet_id) as counts ';
    var sqlFrom = 'from rb_sys_red_packets ';
    var sqlCondition = ' where state = 1';
    sql = sqlSelect + sqlFrom + sqlCondition ;
    console.log(sql);
    sqlClient.query(sql,null, function(err, data){
        if(err){
            console.log(err);
            return cb && cb(err, null);
        }
        return cb && cb(null, data[0].counts);
    })
}

/*
 * 创建系统红包
 *
 * */
rPacketDao.addSysRPacket = function(rPacketType,totalMoney,totalQuantity,timeInterval,taskId,cb){
    var fields=[
        'red_packet_type',
        'total_money',
        'total_quantity',
        'time_interval',
        'task_id'
    ];
    var values=[
        rPacketType,
        totalMoney,
        totalQuantity,
        timeInterval,
        taskId
    ];
    var sql = 'insert into rb_sys_red_packets (' + fields.join(',') + ') values ("' + values.join('","') + '")';
    console.log(sql);
    sqlClient.query(sql,null, function(err, data){
        if(err){
            console.log(err);
            return cb && cb(err, null);
        }
        console.log(data);
        return cb && cb(null, data);
    })
}

rPacketDao.updateSysRPStatus = function(sysRPacketId,status,cb){
    var sql = '';
    var sqlUpdate = 'update rb_sys_red_packets ';
    var sqlSet = 'set status = ' + status;
    var sqlCondition = ' where sys_red_packet_id = ' + sysRPacketId;
    var sql = sqlUpdate + sqlSet + sqlCondition;
    console.log(sql);
    sqlClient.query(sql,null, function(err, data){
        if(err){
            console.log(err);
            return cb && cb(err, null);
        }
        console.log(data);
        return cb && cb(null, data);
    })

}

rPacketDao.changeSysRPState = function(sysRPacketId,cb){
    var sql = '';
    var sqlUpdate = 'update rb_sys_red_packets ';
    var sqlSet = 'set state = 0' ;
    var sqlCondition = ' where sys_red_packet_id = ' + sysRPacketId;
    var sql = sqlUpdate + sqlSet + sqlCondition;
    console.log(sql);
    sqlClient.query(sql,null, function(err, data){
        if(err){
            console.log(err);
            return cb && cb(err, null);
        }
        console.log(data);
        return cb && cb(null, data);
    })

}

rPacketDao.getSysRPDetail = function(sysRPacketId,cb){
    var sql = '';
    var sqlSelect = 'select * ';
    var sqlFrom = 'from rb_sys_red_packets ';
    var sqlCondition = 'where sys_red_packet_id = ' + sysRPacketId;

    sql = sqlSelect + sqlFrom + sqlCondition;
    console.log(sql);
    sqlClient.query(sql,null, function(err, data){
        if(err){
            console.log(err);
            return cb && cb(err, null);
        }
        console.log(data);
        return cb && cb(null, data);
    })
}


rPacketDao.updateRPacket = function(sysRPacketId,totalMoney,totalQuantity,timeInterval,cb){
    var sql = '';
    var sqlUpdate = 'update rb_sys_red_packets ';
    var sqlSet = 'set total_money = ' + totalMoney + ',total_quantity = ' + totalQuantity + ',time_interval = ' + timeInterval;
    var sqlCondition = ' where sys_red_packet_id = ' + sysRPacketId;
    var sql = sqlUpdate + sqlSet + sqlCondition;
    console.log(sql);
    sqlClient.query(sql,null, function(err, data){
        if(err){
            console.log(err);
            return cb && cb(err, null);
        }
        console.log(data);
        return cb && cb(null, data);
    })
}

/*
*
* 更新下次发红包的时间
* */
rPacketDao.updateNextTime = function(sysRPacketId,nextTime,cb){
    var sql = '';
    var sqlUpdate = 'update rb_sys_red_packets ';
    var sqlSet = 'set next_time = "' + nextTime + '"';
    var sqlCondition = ' where sys_red_packet_id = ' + sysRPacketId;
    var sql = sqlUpdate + sqlSet + sqlCondition;
    console.log(sql);
    sqlClient.query(sql,null, function(err, data){
        if(err){
            console.log(err);
            return cb && cb(err, null);
        }
        //console.log(data);
        return cb && cb(null, data);
    })
}


/*
*
* 系统发红包,暂时不生成订单号
*
* */
rPacketDao.addRPacket = function(totalMoney,totalQuantity,redPacketType,taskId,orderNo,cb){
    var fields=[
        'total_money',
        'total_quantity',
        'left_money',
        'left_quantity',
        'red_packet_type',
        'red_packet_order_no',
        'task_id'
    ];
    var values=[
        totalMoney,
        totalQuantity,
        totalMoney,
        totalQuantity,
        redPacketType,
        orderNo,
        taskId

    ];
    var sql = 'insert into rb_red_packets (' + fields.join(',') + ') values ("' + values.join('","') + '")';
    console.log(sql);
    sqlClient.query(sql,null, function(err, data){
        if(err){
            console.log(err);
            return cb && cb(err, null);
        }
        console.log(data);
        return cb && cb(null, data);
    })
}